#ifndef LEETCODE_CN_CPP_PROBLEMS_931_H
#define LEETCODE_CN_CPP_PROBLEMS_931_H

#include <algorithm>
#include <vector>

using namespace std;

namespace problem_931
{

class Solution
{
public:
    int minFallingPathSum(vector<vector<int>> &A)
    {
        if (A.empty())
        {
            return 0;
        }
        int aSize = A.size();
        for (int i = 1; i < aSize; ++i)
        {
            for (int j = 0; j < aSize; ++j)
            {
                int d = A[i - 1][j];
                if (j - 1 >= 0)
                {
                    d = min(d, A[i - 1][j - 1]);
                }
                if (j + 1 < aSize)
                {
                    d = min(d, A[i - 1][j + 1]);
                }
                A[i][j] += d;
            }
        }
        return *(min_element(A.back().begin(), A.back().end()));
    }
};

} // namespace problem_931

#endif //LEETCODE_CN_CPP_PROBLEMS_931_H
